package com.example.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.nacos.shaded.io.grpc.internal.JsonUtil;
import com.example.homework.Show;
import com.example.order.bean.Order;
import com.example.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


//开启nacos配置中心的自动更新
@RefreshScope

////添加网关路径
//@RequestMapping("/api/order")

@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    //创建订单,一个订单有多个商品
    @GetMapping("/create")
    public Order createOrder(@RequestParam("userId") Long userId, @RequestParam("productId") Long productId) {
        Order ordeer = orderService.createOrder(userId, productId);
        return ordeer;
    }

    //测试：多个路径下热点选择，热点模式不支持对web资源的管理，因此需要手动注册为资源
    @SentinelResource(value = "seckill-order-byWJJ",fallback = "seekillFallBack")
    @GetMapping("seekill")
    public Order seekill(@RequestParam(value = "userId",required = false) Long userId,
                         @RequestParam(value = "productId" ,defaultValue = "123") Long productId) {
        Order ordeer = orderService.createOrder(userId, productId);
        ordeer.setNickname("秒杀服务");
        return ordeer;
    }


    public Order seekillFallBack(Long userId, Long productId, Throwable e) {
        Order ordeer = new Order();
        ordeer.setNickname("兜底数据，请求是阿拜");
        return ordeer;
    }


    @GetMapping("/showshow")
    public Show show(@RequestParam("id") Long id) {
        Show show = orderService.show(id);
        return show;
    }



    //测试：获取配置文件
    @Value("${order.timeout}")
    String timeout;
    @Value("${order.auto-confirm}")
    String autoConfirm;
    @GetMapping("/config")
    public String config() {
        return timeout + " " + autoConfirm;
    }
}
